package arkham.data;

import java.awt.Color;

import arkham.data.Globals.InvestigatorCardType;
import arkham.jaxb.encounter.CardType;
import arkham.util.AHLogger;
import arkham.util.XmlTools;

public class Spell extends InvestigatorCard {

	private int hands;
	private int sanityCost;
	private int castingModifier;
	private boolean combatSpell;
	
	public Spell(String name)
	{
		super(name);
	}

	@Override
	public InvestigatorCardType getType() 
	{
		return InvestigatorCardType.SPELL;
	}
	
	@Override
	public boolean isItem()
	{
		return true;
	}
	
	/**
	 * Gets the sanity cost of the spell
	 * @return The sanity cost
	 */
	public int getSanityCost()
	{
		return sanityCost;
	}
	
	/**
	 * Gets the casting modifier
	 * @return The casting modifier
	 */
	public int getCastingModifier()
	{
		return castingModifier;
	}

	/**
	 * Gets the number of hands this spell uses
	 * @return The number of hands the spell uses
	 */
	public int getHands()
	{
		return hands;
	}
	
	/**
	 * Checks if this card is a combat spell
	 * @return true if it is, false otherwise
	 */
	public boolean isCombatSpell()
	{
		return combatSpell;
	}
	
	public Color getPreferredColor()
	{
		return new Color(224, 224, 234);
	}
	
	/**
	 * This method creates a spell card from the appropriate XML info
	 * @param xmlcard The XML data
	 * @return The data representation of the object
	 */
	public static Spell createCardFromXml(CardType xmlcard)
	{
		Spell spell = new Spell(xmlcard.getName());
		spell.loadCommonFieldsFromXml(xmlcard);
		
		/* Get the cost of the card */
		int cost = -2;
		try
		{
			cost = XmlTools.getIntPropertyValue(xmlcard, "Sanity Cost");
		}
		catch (NumberFormatException e)
		{
			AHLogger.error(e.getMessage());
			return null;
		}
		if (cost < -1)
		{
			AHLogger.error(spell.getName() + " has an invalid sanity cost " + cost);
			return null;
		}
		spell.sanityCost = cost;
		
		/* Get casting modifier */
		int mod = 9999;
		try
		{
			mod = XmlTools.getIntPropertyValue(xmlcard, "Casting Modifier");
		}
		catch (NumberFormatException e)
		{
			AHLogger.error(e.getMessage());
			return null;
		}
		if (mod < -1000 || mod > 1000)
		{
			AHLogger.error(spell.getName() + " has an invalid casting modifier " + mod);
			return null;
		}
		spell.castingModifier = mod;
		
		/* Get number of hands */
		if (XmlTools.isPropertyDefined(xmlcard, "Hands"))
		{
			int hands = -1;
			try
			{
				hands = XmlTools.getIntPropertyValue(xmlcard, "Hands");
			}
			catch (NumberFormatException e)
			{
				AHLogger.error(e.getMessage());
				return null;
			}
			if (hands < 0 || hands > 2)
			{
				AHLogger.error(spell.getName() + " has an invalid number of hands: " + hands);
				return null;
			}
			spell.hands = hands;
		}
		
		spell.combatSpell = false;
		if (XmlTools.isPropertyDefined(xmlcard, "Combat Spell"))
		{
			if (XmlTools.getStrPropertyValue(xmlcard, "Combat Spell").equalsIgnoreCase("true"))
			{
				spell.combatSpell = true;
			}
		}
		
		return spell;
	}
	
}
